package processor;

import com.sun.org.apache.bcel.internal.generic.Select;
import model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.HtmlNode;
import us.codecraft.webmagic.selector.Selectable;
import util.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

//import static processor.LoginProcessor.isRedirectedToLoginPage;
//import static processor.LoginProcessor.logger;

public class BasePageProcessor {
    public static Logger logger = LoggerFactory.getLogger("BasePageProcessor");

//    public static BaseBusinessInfo ParseBBSI(Page page){
//        /**
//         * @Description 抓取组织的基本信息
//         */
//        BaseBusinessInfo baseBusinessInfo = new BaseBusinessInfo();
//        String unique = page.getUrl().toString().substring(page.getUrl().toString().lastIndexOf("firm_"),page.getUrl().toString().lastIndexOf(".html")).replace("firm_","");
//        baseBusinessInfo.setUnique(unique);//公司url
//        baseBusinessInfo.setSearch_word(page.getRequest().getExtra("search_word").toString());//当前搜索词
//        List<Selectable> node = page.getHtml().xpath("//section[@id='Cominfo']//table[@class=\"ntable\"]//td[@class!=\"tb\"]").nodes();
//        if (node.size()>0) {
//            baseBusinessInfo.setCompany_name(page.getHtml().xpath("//div[@class='row title']//h1/allText()").toString());//公司名称
//            baseBusinessInfo.setCredit_code(node.get(0).xpath("/td/allText()").toString());
//            baseBusinessInfo.setLegal_representative(node.get(1).xpath("/td/allText()").toString());
//            baseBusinessInfo.setRegistered_capital(node.get(2).xpath("/td/allText()").toString());
//            baseBusinessInfo.setEstablishment_date(node.get(3).xpath("/td/allText()").toString());
//            baseBusinessInfo.setOperating_status(node.get(4).xpath("/td/allText()").toString());
//            baseBusinessInfo.setCompany_type(node.get(5).xpath("/td/allText()").toString());
//            baseBusinessInfo.setRegistration_authority(node.get(6).xpath("/td/allText()").toString());
//            baseBusinessInfo.setApproved_date(node.get(7).xpath("/td/allText()").toString());
//            baseBusinessInfo.setBusiness_scope(node.get(8).xpath("/td/allText()").toString());
//            baseBusinessInfo.setCompany_address(node.get(9).xpath("/td/allText()").toString());
//
//        }
//        page.putField("unique",unique);
//
//        return baseBusinessInfo;
//    }

    // 唯一标识
    public static BaseBusinessInfo ParseBBI(Page page){
        /**
         * @Description 抓取基本信息
         */
        BaseBusinessInfo baseBusinessInfo = new BaseBusinessInfo();
        String unique = page.getUrl().toString().substring(page.getUrl().toString().lastIndexOf("firm_"),page.getUrl().toString().lastIndexOf(".html")).replace("firm_","");
        baseBusinessInfo.setUnique(unique);//公司url
        baseBusinessInfo.setSearch_word(page.getRequest().getExtra("search_word").toString());//当前搜索词
//        LogToKafka logToKafka = new LogToKafka();
        //抓取 产品信息
//        List<Selectable> dongtai = page.getHtml().xpath("//a[@class='oxin']/@href").nodes();
//        for (Selectable i:dongtai){
//            if (i.toString().startsWith("/product_")){
//                baseBusinessInfo.setProduct_url(i.toString()
//                .replace("/","")
//                .replace(".html",""));
//                // 产品优先级 -1
//                addTargetRequest.add(page,
//                        "https://www.qichacha.com"+i.toString(),
//                        page.getRequest().getExtra("search_word").toString(),
//                        page.getRequest().getPriority()-1
//                        );
//            }
//        }
        //抓取 工商信息
        List<Selectable> node = page.getHtml().xpath("//section[@id='Cominfo']//table[@class=\"ntable\"]//td").nodes();

        if (node.size()>0 && node.size()%2==0){
            HashMap<String,Selectable> mapper = new HashMap<>();
            for(int index=0;index<node.size();index+=2){
                mapper.put(node.get(index).xpath("/td/allText()").toString(),
                        node.get(index+1));
            }
            baseBusinessInfo.setCompany_name(MyString.Replace(page.getHtml().xpath("//div[@class='row title jk-tip']/h1/allText()").toString()));//公司名称

            try{
                // 区分 法定代表人和执行事务合伙人两种情况。
                baseBusinessInfo.setLegal_representative_unique(page.getHtml().xpath("//div[@class='boss-td']//a/@href").toString()
                        .replace("firm_","")
                        .replace("/","")
                        .replace(".html",""));//法定代表人id
                baseBusinessInfo.setLegal_representative(page.getHtml().xpath("//div[@class='boss-td']//a/allText()").toString());//法定代表人

            }
            catch(Exception e){
//                e.printStackTrace();
                logger.error(e.getStackTrace().toString());
            }
            String offical_url = "";
//            List<Selectable> selectables = page.getHtml().xpath("//div[@class='dcontent']/div").nodes()
//                    .get(0).xpath("/div/span").nodes();
//            if(selectables.size()>0){
//                offical_url = selectables.get(selectables.size()-1).xpath("/span/allText()").toString();
//            }
            offical_url = page.getHtml().xpath("//div[@class='dcontent']//a[@title='进入官网']/@href").toString();
            baseBusinessInfo.setOffical_url(offical_url);//官网信息
            baseBusinessInfo.setRegistered_capital(mapper.get("注册资本").xpath("/td/allText()").toString());//注册资本
            baseBusinessInfo.setPaid_capital(mapper.get("实缴资本").xpath("/td/allText()").toString());//实缴资本
            baseBusinessInfo.setOperating_status(mapper.get("经营状态").xpath("/td/allText()").toString());//经营状态
            baseBusinessInfo.setEstablishment_date(mapper.get("成立日期").xpath("/td/allText()").toString());//成立日期
            baseBusinessInfo.setCredit_code(mapper.get("统一社会信用代码").xpath("/td/allText()").toString());//统一社会信用代码
            baseBusinessInfo.setTaxpayer_number(mapper.get("纳税人识别号").xpath("/td/allText()").toString());//纳税人识别号
            baseBusinessInfo.setRegistration_number(mapper.get("注册号").xpath("/td/allText()").toString());//注册号
            baseBusinessInfo.setOrganization_code(mapper.get("组织机构代码").xpath("/td/allText()").toString());//组织机构代码
            baseBusinessInfo.setCompany_type(mapper.get("企业类型").xpath("/td/allText()").toString());//企业类型
            baseBusinessInfo.setIndustry(mapper.get("所属行业").xpath("/td/allText()").toString());//所属行业
            baseBusinessInfo.setApproved_date(mapper.get("核准日期").xpath("/td/allText()").toString());//核准日期
            baseBusinessInfo.setRegistration_authority(mapper.get("登记机关").xpath("/td/allText()").toString());//登记机关
            baseBusinessInfo.setDistrict(mapper.get("所属地区").xpath("/td/allText()").toString());//所属地区
            baseBusinessInfo.setEnglish_name(mapper.get("英文名").xpath("/td/allText()").toString());//英文名
            baseBusinessInfo.setUsed_name(mapper.get("曾用名").xpath("/td/allText()").toString());//曾用名
            baseBusinessInfo.setInsured_person(mapper.get("参保人数").xpath("/td/allText()").toString());//参保人数
            baseBusinessInfo.setPerson_size(mapper.get("人员规模").xpath("/td/allText()").toString());//人员规模
            baseBusinessInfo.setOperating_period(mapper.get("营业期限").xpath("/td/allText()").toString());//营业期限
            baseBusinessInfo.setCompany_address(mapper.get("企业地址").xpath("/td/allText()").toString());//企业地址
            if (baseBusinessInfo.getCompany_address() != null){
                String r = baseBusinessInfo.getCompany_address().replace(" 查看地图 附近企业","");
                baseBusinessInfo.setCompany_address(r);
            }
            baseBusinessInfo.setBusiness_scope(mapper.get("经营范围").xpath("/td/allText()").toString());//经营范围
            baseBusinessInfo.setCreatedTime(Mytime.getTime());//创建时间

            page.putField("company_name", MyString.Replace(baseBusinessInfo.getCompany_name()));

        }
        page.putField("unique",unique);
//        logToKafka.log(new CrawlerLog(page.getRequest().getExtra("job_id").toString(),
//                page.getRequest().getExtra("search_word").toString(),
//                "Downloaded_baseBusinessInfo",
//                unique,
//                Mytime.getTime()));
        logger.info(baseBusinessInfo.getCompany_name()+" 基本信息解析完成。unique: "+baseBusinessInfo.getUnique());
        return baseBusinessInfo;
    }
    public static List<BaseShareholderInfo> ParseBSI(Page page){
        /**
         * @Description 抓取股东信息
         */
        List<BaseShareholderInfo> baseShareholderInfos = new ArrayList<BaseShareholderInfo>();
        List<Selectable> nodes = page.getHtml().xpath("//section[@id='partnerslist']/table/tbody/tr").nodes();
        if (nodes.size() > 1){
            for (int i = 1;i<nodes.size();i++){
                BaseShareholderInfo baseShareholderInfo = new BaseShareholderInfo();
                baseShareholderInfo.setCompany_unique(page.getResultItems().get("unique").toString());
                try {
                    baseShareholderInfo.setShareholder_unique(nodes.get(i).xpath("/tr/td").nodes().get(1).xpath("td//td").nodes().get(1).xpath("//a/@href").toString()
                            .replace("firm_","")
                            .replace(".html","")
                            .replace("/",""));
                }catch (Exception e){
                    logger.error(e.getStackTrace().toString());
                }
                // 使用HashMap记录表头与每一个表项
                HashMap<String,String> shareholderMap = new HashMap<>();
                // 表头。
                List<Selectable> th = nodes.get(0).xpath("/tr/th").xpath("/allText()").nodes();
                // 表项
                List<Selectable> td = nodes.get(i).xpath("/tr/td").xpath("/allText()").nodes();
                // 保证表头与表项个数一致
                if (th.size() == td.size()){

                    for (int index=0;index<th.size();index++){
                        // 将表头与表项放入到字典中，同时以“ ”为分隔符，保留正确的显示文本
                        shareholderMap.put(th.get(index).toString().split(" ")[0],td.get(index).toString().split(" ")[0]);
                    }
                }
                else {
                    System.out.println("股东关系表头与表项不一致，退出该步骤抓取！");
                    break;
                }
                // 股东信息 的表头不固定，因此使用序号
                baseShareholderInfo.setShareholder(MyString.Replace(td.get(1).toString().split(" ")[0]));
                baseShareholderInfo.setShareholding_ratio(shareholderMap.get("持股比例"));
                baseShareholderInfo.setSubscription_amount(shareholderMap.get("认缴出资额(万元)"));
                baseShareholderInfo.setFunding_date(shareholderMap.get("认缴出资日期"));
                baseShareholderInfo.setCreatedTime(Mytime.getTime());
                baseShareholderInfos.add(baseShareholderInfo);
                // 公司股东的情况下进行请求
                if (baseShareholderInfo.getShareholder_unique() != null &&!baseShareholderInfo.getShareholder_unique().startsWith("pl_")){
                    //如果当前优先级大于边界优先级 抓取下一层级股东关系
                    if (page.getRequest().getPriority()-1>=Integer.valueOf(page.getRequest().getExtra("InitialPriority").toString())
                            -Integer.valueOf(page.getRequest().getExtra("MinShareholderPriority").toString())){
                        addTargetRequest.add(page,
                                "https://www.qichacha.com/firm_"+baseShareholderInfo.getShareholder_unique()+".html#base",
                                baseShareholderInfo.getShareholder(),
                                page.getRequest().getPriority()-1);
                    }

                }

//                System.out.println("股东信息抓去完成");
            }
        }
        return baseShareholderInfos;
    }
    public static List<BaseMainStaff> ParseBMS(Page page){
        /**
         * @Description 抓取主要人员 信息
         *
         */
        List<BaseMainStaff> baseMainStaffs = new ArrayList<BaseMainStaff>();
        List<Selectable> nodes = page.getHtml().xpath("//section[@id='Mainmember']//tr").nodes();
        if (nodes.size() > 1){
            for (int i = 1;i<nodes.size();i++){
                BaseMainStaff baseMainStaff = new BaseMainStaff();
                baseMainStaff.setCompany_unique(page.getResultItems().get("unique").toString());
                baseMainStaff.setNameUnique(nodes.get(i).xpath("//a[@class='c_a']/@href").toString()
                .replace("/","")
                .replace(".html",""));
                baseMainStaff.setName(nodes.get(i).xpath("//h3/allText()").toString());
                baseMainStaff.setDuty(nodes.get(i).xpath("//td[@class='text-center']/allText()").toString());
                baseMainStaff.setCreatedTime(Mytime.getTime());
                baseMainStaffs.add(baseMainStaff);
            }
        }
//        System.out.println("主要人员抓取完成");

        return baseMainStaffs;
    }

    public static List<BaseChange> ParseBC(Page page){
        /**
         * 抓取历史变更信息。
         */
        // 解析
        List<BaseChange> baseChanges = new ArrayList<BaseChange>();
        List<Selectable> nodes = page.getHtml().xpath("//section[@id='Changelist']//tr").nodes();
        if (nodes.size() > 1){
            for (int i = 1;i<nodes.size();i++){
                String number = nodes.get(i).xpath("tr/td").nodes().get(0).xpath("td/allText()").toString();
                String change_date = nodes.get(i).xpath("tr/td").nodes().get(1).xpath("td/allText()").toString();
                String change_project = nodes.get(i).xpath("tr/td").nodes().get(2).xpath("td/allText()").toString();
                String before_text = nodes.get(i).xpath("tr/td").nodes().get(3).xpath("td/allText()").toString();
                List<Selectable> tr3 = nodes.get(i).xpath("tr/td").nodes().get(3).xpath("td//em").nodes();
                for (int before_i = 0;before_i<tr3.size();before_i++){
                    String before_text_em = tr3.get(before_i).xpath("em//a/allText()").toString();
                    String before_text_link = "";
                    try{
                        before_text_link = tr3.get(before_i).xpath("em//a/@href").toString().replace("firm_","")
                                .replace(".html","")
                                .replace("/","");
                        // 变更之前的公司信息需要抓取基本信息
                        if (!before_text_link.startsWith("pl_")){
                                //如果当前优先级大于边界优先级 抓取下一层级变更关系
                                if (page.getRequest().getPriority()-1>=Integer.valueOf(page.getRequest().getExtra("InitialPriority").toString())
                                        -Integer.valueOf(page.getRequest().getExtra("").toString())){
                                    addTargetRequest.add(page,
                                            "https://www.qichacha.com/firm_"+before_text_link+".html#base",
                                            before_text_em,
                                            page.getRequest().getPriority()-1);
                                }

                        }
                    }catch (NullPointerException  np){

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    BaseChange baseChange = new BaseChange();
                    baseChange.setCompany_unique(page.getResultItems().get("unique").toString());
                    baseChange.setBefore_id(String.valueOf(before_i));
                    baseChange.setBefore_text(before_text);
                    baseChange.setBefore_text_em(before_text_em);
                    baseChange.setBefore_text_link(before_text_link);
                    baseChange.setNumber(number);
                    baseChange.setChange_date(change_date);
                    baseChange.setChange_project(change_project);
                    baseChange.setCreatedTime(Mytime.getTime());
                    baseChanges.add(baseChange);
                }
                String after_text = nodes.get(i).xpath("tr/td").nodes().get(4).xpath("td/allText()").toString();

                List<Selectable> tr4 = nodes.get(i).xpath("tr/td").nodes().get(4).xpath("td//em").nodes();
                for (int after_id = 0;after_id<tr4.size();after_id++){
                    String after_text_em = tr4.get(after_id).xpath("em//a/allText()").toString();
                    String after_text_link = "";
                    try{
                        after_text_link = tr4.get(after_id).xpath("em//a/@href").toString().replace("firm_","")
                                .replace(".html","")
                                .replace("/","");
                        // 变更之后的公司信息需要抓取基本信息
                        if (!after_text_link.startsWith("pl_")){
                            //如果当前优先级大于边界优先级 抓取下一层级股东关系
                            if (page.getRequest().getPriority()-1>=
                                    Integer.valueOf(page.getRequest().getExtra("InitialPriority").toString())
                                            -Integer.valueOf(page.getRequest().getExtra("MinChangePriority").toString())){
                                addTargetRequest.add(page,
                                        "https://www.qichacha.com/firm_"+after_text_link+".html#base",
                                        after_text_em,
                                        page.getRequest().getPriority()-1);
                            }

                        }
                    }catch (NullPointerException  np){

                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    BaseChange baseChange = new BaseChange();
                    baseChange.setCompany_unique(page.getResultItems().get("unique").toString());
                    baseChange.setAfter_id(String.valueOf(after_id));
                    baseChange.setAfter_text(after_text);
                    baseChange.setAfter_text_em(after_text_em);
                    baseChange.setAfter_text_link(after_text_link);
                    baseChange.setNumber(number);
                    baseChange.setChange_date(change_date);
                    baseChange.setChange_project(change_project);
                    baseChange.setCreatedTime(Mytime.getTime());
                    baseChanges.add(baseChange);
                }
            }
        }
        return baseChanges;
    }
    public static void processFirmBase(Page page){
//        System.out.println("开始解析基本信息");
//        if(isRedirectedToLoginPage(page)){
//            return;
//        }
//        if(page.getUrl().toString().contains("firm_s")){
//            page.putField("BaseBusinessInfo",ParseBBSI(page));
//
//        }else {
            page.putField("BaseBusinessInfo",ParseBBI(page));

//        }
        page.putField("BaseMainStaff",ParseBMS(page));
        page.putField("BaseShareholderInfo",ParseBSI(page));
        page.putField("BaseChange",ParseBC(page));
    }
}
